package jvms.reflection;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * Created by pc on 2015/5/4.
 */

class Demo {
    private String name = "hello";
}

class Person {
    //用newInstance不能只有这个构造函数
    public Person(String name, int age) {
        this.age=age;
        this.name=name;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "[" + this.name + "  " + this.age + "]";
    }
    private String name;
    private int age;
}

public class ReflectionDemo {

    public static void main(String[] args) {
        Class<?> demo1 = null;
        Class<?> demo2 = null;
        Class<?> demo3 = null;
        try {
            //一般尽量采用这种形式
            demo1 = Class.forName("jvms.reflection.Demo");
        } catch (Exception e) {
            e.printStackTrace();
        }
        demo2 = new Demo().getClass();
        demo3 = Demo.class;

        //class.forName生成的不是对实例，而是类对象
        System.out.println("类名称   " + demo1.getName() + ", real: " + demo1.hashCode());
        System.out.println("类名称   " + demo2.getName() + ", real: " + demo2.hashCode());
        System.out.println("类名称   " + demo3.getName() + ", real: " + demo3.hashCode());
        System.out.println(demo1 == demo2);
        System.out.println(demo2 == demo3);
        System.out.println(demo1);

        //new instance才是类实例
        try {
            System.out.println(demo1.newInstance() == demo2.newInstance());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }



        // 另一种方法用反射新建对象
        Class<?> clazz=null;
        Person per=null;
        try {
            clazz = Class.forName("jvms.reflection.Person");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("===============本类属性========================");
        // 取得本类的全部属性
        Field[] field = clazz.getDeclaredFields();
        for (int i = 0; i < field.length; i++) {
            // 权限修饰符
            int mo = field[i].getModifiers();
            String priv = Modifier.toString(mo);
            // 属性类型
            Class<?> type = field[i].getType();
            System.out.println(priv + " " + type.getName() + " "
                    + field[i].getName() + ";");
        }
        System.out.println("===============实现的接口或者父类的属性========================");
        // 取得实现的接口或者父类的属性
        Field[] filed1 = clazz.getFields();
        for (int j = 0; j < filed1.length; j++) {
            // 权限修饰符
            int mo = filed1[j].getModifiers();
            String priv = Modifier.toString(mo);
            // 属性类型
            Class<?> type = filed1[j].getType();
            System.out.println(priv + " " + type.getName() + " "
                    + filed1[j].getName() + ";");
        }




        try {
            per=(Person)clazz.newInstance();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        per.setName("Rollen");
        per.setAge(20);
        System.out.println(per);
    }
}
